#include "sagevistextlabel.h"

SageVisTextLabel::SageVisTextLabel(QPointer<SageVis> sageVisPointer, QPointer<VisBaseWidget> parentWidget, QGraphicsItem *parent ) :
    VisBaseElement( sageVisPointer, parentWidget, parent)
{
    init("");//setup text label with default empty text
}


SageVisTextLabel::SageVisTextLabel(QString t, QPointer<SageVis> sageVisPointer, QPointer<VisBaseWidget> parentWidget, QGraphicsItem *parent ) :
    VisBaseElement( sageVisPointer, parentWidget, parent)
{
    init(t);//setup text label with text param
}

void SageVisTextLabel::init( QString t )
{
    textString = t;

    setAcceptHoverEvents(true);//when hovering over a label, make it more prominent
    hovering = false;//currently not hovering

    defaultHeight = 5.0;
    maximizedHeight = 15.0;

    setupText();
    setCacheMode(QGraphicsItem::ItemCoordinateCache);//this call is magic- render to pixmap makes it faster

    //uniqueId = "";
}


void SageVisTextLabel::setText(QString t )
{
    textString = t;
    setupText();
}

//void SageVisTextLabel::setUniqueId(QString id)
//{
//    uniqueId = id;
//}

int SageVisTextLabel::getTextWidth()
{
    return textWidth;
}

void SageVisTextLabel::setLabelWidth(int labelWidth)
{
    this->labelWidth = labelWidth;
}

QString SageVisTextLabel::getUniqueId()
{
    return textString; //TO DO:  not really unique.  Good enough?
}

void SageVisTextLabel::setupText()
{
    //SETUP TEXT, FONT, TEXT SIZE, Z VALUE

    font = new QFont( "Helvetica [Cronyx]", 12);
    font->setPixelSize(14);//setPointSize( 13 );//for now

    //init text width- at large text size.
    //QPainter painter;
    //painter.setFont(*font);
    QFontMetrics fm(*font);//painter.fontMetrics();//font metrics allows us to compute text width
    textWidth = fm.width(textString);//gives calculated text width with current QFont in QPainter

    //set geometry, so that line connection will work
    setGeometry( this->geometry().x(), this->geometry().y(), textWidth*1.05, this->geometry().height() );
}

void SageVisTextLabel::setGeometry(const QRectF &geom)
{    
    super::setGeometry(geom);
}

void SageVisTextLabel::setGeometry(qreal x, qreal y, qreal w, qreal h)
{
    setGeometry(QRectF( x, y, w, h ));
}

void SageVisTextLabel::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    float newHeight = geometry().height();

        defaultHeight = newHeight; //store old height
        newHeight *= 3.0;//increase size if hovering
        if( newHeight > maximizedHeight )//ensure it doesn't get too big
            newHeight = maximizedHeight;


    setHeight( newHeight );//set the height

    //qDebug() << "text label emit hover event" << endl;

    super::processHoverEvent(event); //call base class to ensure hover event goes through

    emit( recomputeLayoutSignal() ); //with height change, must recompute layout
}

void SageVisTextLabel::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    float newHeight = geometry().height();

        newHeight = defaultHeight;//restore default //3.0;//decrease size if no longer hovering

    setHeight( newHeight );//set the height

    //qDebug() << "text label emit hover event" << endl;

    super::processHoverEvent(event); //call base class to ensure hover event goes through

    emit( recomputeLayoutSignal() ); //with height change, must recompute layout
}

//reimplement
void SageVisTextLabel::processHoverEvent(QGraphicsSceneHoverEvent *event)
{   
//    float newHeight = geometry().height();
//    if( !hovering )//not hovering now, will soon
//    {
//        defaultHeight = newHeight; //store old height
//        newHeight *= 3.0;//increase size if hovering
//        if( newHeight > maximizedHeight )//ensure it doesn't get too big
//            newHeight = maximizedHeight;
//    }
//    else
//    {
//        newHeight = defaultHeight;//restore default //3.0;//decrease size if no longer hovering
//    }

//    setHeight( newHeight );//set the height

//    //qDebug() << "text label emit hover event" << endl;

//    super::processHoverEvent(event); //call base class to ensure hover event goes through

//    emit( recomputeLayoutSignal() ); //with height change, must recompute layout
}

void SageVisTextLabel::receiveHoverEvent(QGraphicsSceneHoverEvent *event)
{
    //qDebug() << "receive hover signal in text label. " << textString << endl;

    //super::processHoverEvent(event);//changing... wonder why it was this way.
    super::receiveHoverEvent(event);

    emit( recomputeLayoutSignal() ); //with height change, must recompute layout

}



void SageVisTextLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
           QWidget *widget)
{
    //get updated label height
        //note: not in change height because of layout bug
    int textSize = (int) geometry().height(); //- 2; //current font size
    //I took this out b/c need to center in bounding box if you scale text like this
    if( textSize%2 != 0 )
        textSize--;
    if( textSize > 12 )
        textSize = 12;
    if( textSize < 2 )
        textSize = 2;

    font->setPixelSize(textSize);

    //must recompute text width with active painter
    painter->setFont(*font);
    QFontMetrics fm = painter->fontMetrics();//font metrics allows us to compute text width
    int newTextWidth = fm.width(textString);//gives calculated text width with current QFont in QPainter

    //reset geometry if necessary
    if( newTextWidth != textWidth )
    {
        textWidth = newTextWidth;
        setGeometry( this->geometry().x(), this->geometry().y(), textWidth*1.05, this->geometry().height() );
        emit(updatedSignal());
    }

    QColor c;
    c.setRgb(255.0, 255.0, 255.0);
    painter->setPen(c);
    if( textSize < geometry().height()-2 )
    {
        //find center
        float centY = boundingRect().y()+boundingRect().height()/2.0;
        float diff = boundingRect().height() - textSize;
        QRectF textbox(boundingRect().x(), (boundingRect().y() + diff/2.0), boundingRect().width(), textSize  );
        if( DISPLAY_MODE == WALL_MODE )
            textbox.setCoords(boundingRect().x(), boundingRect().y()-diff/2.0, boundingRect().width(), textSize );

        painter->drawText(textbox, textString);
        //   FOR DEBUGGING
//        c.setRgb(255.0, 0.0, 0.0);
//        painter->setPen(c);
//        painter->drawRect(textbox);
    }
    else
    {
        painter->drawText(boundingRect(), textString);
        // FOR DEBUGGING
//        c.setRgb(0.0, 255.0, 0.0);
//        painter->setPen(c);
//        painter->drawRect(boundingRect());
    }

        //FOR DEBUGGING
//    painter->setBrush(Qt::white );
//    QPointF lcp = mapFromParent(getLineConnectionPoint() );
//    painter->drawRect( lcp.x()-2.0, lcp.y()-2.0, 4.0, 4.0 );

    //FOR DEBUGGING
//    QColor c;
//    c.setRgb(255.0, 0.0, 0.0);
//    painter->setPen(c);
//    painter->drawRect(boundingRect().x(), boundingRect().y(), textWidth*1.05, boundingRect().height());

//    QColor c2;
//    c2.setRgb(0.0, 255.0, 0.0);
//    painter->setPen(c2);
//    painter->drawRect(text->boundingRect().x(), text->boundingRect().y(), text->boundingRect().width(), text->boundingRect().height());

}

